package com.yanqu.road.dao.impl.activity.dragonboatrace;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.dragonboatrace.DragonBoatRaceUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author cwq
 * @Data 2021/5/14 17:40
 */
public class DragonBoatRaceUserDataDaoImpl  extends TempDataObjectDao<DragonBoatRaceUserData> {

    public static String getDragonBoatRaceUserDataSql = "select * from `t_u_activity_dragonboatrace_role_data` where `activityId`=? and `userId`=?";
    public DragonBoatRaceUserData getData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getDragonBoatRaceUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parseData(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateDragonBoatRaceUserDataSql = "update `t_u_activity_dragonboatrace_role_data` set times=?,placeId=?,lastTimeJoinTime=?,patronsList=? where activityId=? and userId=?";
    public boolean updateData(DragonBoatRaceUserData dragonBoatRaceUserData) {
        dragonBoatRaceUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, dragonBoatRaceUserData.getTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, dragonBoatRaceUserData.getPlaceId()));
        para.put(i++, new DbParameter(Types.INTEGER, dragonBoatRaceUserData.getLastTimeJoinTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(dragonBoatRaceUserData.getPatronsList())));
        para.put(i++, new DbParameter(Types.INTEGER, dragonBoatRaceUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, dragonBoatRaceUserData.getUserId()));
        if (getDb().executeNoneQuery(updateDragonBoatRaceUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertDragonBoatRaceUserDataSql = "insert into `t_u_activity_dragonboatrace_role_data`(activityId,userId,times,placeId,lastTimeJoinTime,patronsList) values (?,?,?,?,?,?)";
    public boolean addData(DragonBoatRaceUserData dragonBoatRaceUserData) {
        dragonBoatRaceUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, dragonBoatRaceUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, dragonBoatRaceUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, dragonBoatRaceUserData.getTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, dragonBoatRaceUserData.getPlaceId()));
        para.put(i++, new DbParameter(Types.INTEGER, dragonBoatRaceUserData.getLastTimeJoinTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(dragonBoatRaceUserData.getPatronsList())));
        if (getDb().executeNoneQuery(insertDragonBoatRaceUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    protected DragonBoatRaceUserData parseData(ResultSet rs) throws SQLException {
        DragonBoatRaceUserData dragonBoatRaceUserData = new DragonBoatRaceUserData();
        dragonBoatRaceUserData.setActivityId(rs.getInt("activityId"));
        dragonBoatRaceUserData.setUserId(rs.getLong("userId"));
        dragonBoatRaceUserData.setTimes(rs.getInt("times"));
        dragonBoatRaceUserData.setPlaceId(rs.getInt("placeId"));
        dragonBoatRaceUserData.setLastTimeJoinTime(rs.getInt("lastTimeJoinTime"));
        dragonBoatRaceUserData.setPatronsList(JSON.parseObject(rs.getString("patronsList"), new TypeReference<List<Integer>>() {}));

        return dragonBoatRaceUserData;
    }

}
